#include <project.h>
/*-------------------------------------------------------------------------

***************** Modification History ***********************************
   @doc
   @module dmp.c - Packet load and save functions for debug purposes |
   @end
   
$Log: /SoftPay/SPK2SIM/Source/Utils/dmp.c $ 
 * 
 * 5     2/21/00 3:02p Jim_b4
 * Include file restructuring
 * 
 * 4     2/21/00 11:30a Jim_b4
 * Include directory restructuring
 * 
 * 3     1/26/00 6:43p Page_k1
 * Remove first parameter from vdGetMessageFromFile and
 * vdDisplayMessageFromFile
 * 
 * 2     9/13/99 8:30a Chris_s1
 * Changes made to primarily prototypes to conform to the Motorola 68000
 * format...
 * 
 * 1     7/19/99 5:45p Jim_b4
 * 
 * 1     7/19/99 4:31p Jim_b4
 * 
 * 4     6/24/99 4:14p Nick_t1
 * 
 * 3     6/10/99 3:25p Nick_t1
 * 
 * 2     6/09/99 11:22a Monique_b1
 * Change headers and file names for SP 2000
**************************************************************************

   Copyright (C) 1999 by VeriFone, Inc.

   All rights reserved.  No part of this software may be reproduced,
   transmitted, transcribed, stored in a retrieval system, or translated
   into any language or computer language, in any form or by any means,
   electronic, mechanical, magnetic, optical, chemical, manual or otherwise,
   without the prior written permission of VeriFone, Inc.

---------------------------------------------------------------------------*/
#define VSDMP_C

#include <stdio.h>
#include <message.h>

#if defined (_VSC)
// #include <txoio.h>
#endif

#ifdef DEBUG_VSISODMP
#define DEBUG_95
#endif

#include <define.h>
#include <comm.h>
#include <msg.h>
#include <msgfuncs.h>
#include <ui.h>
#include <dmp.h>
#include <spdebug.h>
#include <util.h>

#define REQPKT_FNAME "VSREQ.PKT"
#define RSPPKT_FNAME "VSRSP.PKT"

int inSavePacket(int inPacketType,char* pchPkt,int inSize) {
    FHANDLE hFHandle;
    char szFName[FILENAME_SIZE + 1];

    pdebug(("--inSavePacket--"));

    if (inSize <= 0)
        return(VS_ERR);

    switch (inPacketType) {
    case REQUEST_PACKET:
        strlcpy(szFName, REQPKT_FNAME, FILENAME_SIZE);
        break;
    case RESPONSE_PACKET:
        strlcpy(szFName, RSPPKT_FNAME, FILENAME_SIZE);
        break;
    }

    hFHandle = open(szFName, (O_CREAT | O_TRUNC | O_RDWR));

    if (write(hFHandle, pchPkt, inSize) != inSize) {
        close(hFHandle);
        return(VS_ERR);
    }

    if (close(hFHandle) < 0)
        return(VS_ERR);

    return(VS_SUCCESS);
}


/*
 * Tries to read GEN_BUF_SIZE bytes using pchPkt, so pchPkt should be atleast
 * GEN_BUF_SIZE in length
 */
int inLoadPacket(int inPacketType,char* pchPkt,int* pinSize) {
    FHANDLE hFHandle;
    char szFName[FILENAME_SIZE + 1];

    pdebug(("--inLoadPacket--"));

    switch (inPacketType) {
    case REQUEST_PACKET:
        strlcpy(szFName, REQPKT_FNAME, FILENAME_SIZE);
        break;
    case RESPONSE_PACKET:
        strlcpy(szFName, RSPPKT_FNAME, FILENAME_SIZE);
        break;
    }

    if ((hFHandle = open(szFName, O_RDWR)) < 0) {
        vdDisplayMessageFromFile(NO_DMP_ERR_MSG, ERROR_MSG);
        return(VS_FAILURE);
    }

    if ((*pinSize = read(hFHandle, (char *) pchPkt, GEN_BUF_SIZE)) <= 0) {
        close(hFHandle);
        vdDisplayMessageFromFile(NO_DMP_ERR_MSG, ERROR_MSG);
        return(VS_FAILURE);
    }

    if (close(hFHandle) < 0)
        return(VS_FAILURE);

    return(VS_SUCCESS);
}

#undef VSDMP_C

